// pages/subrogation/gfPayment/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '好友代付',
            'color': true,
            'class': false
        },
        userInfo: wx.getStorageSync('userInfo'),
        dialogOne: false,
        dialogTwo: false,
        dialogThree: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options)
        this.setData({
            type: options.type,
            money: Number(options.title),
            orderNo: options.orderNo,
            title: this.data.userInfo.nickName + '申请你帮他付' + options.title + '元'
        })
        // if (options.type == 2) {
        //     this.mandate()
        // }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},

    // indent() {
    //     var that = this,
    //         data = {
    //             "transactionMoney": that.data.money,
    //             "deptId": that.data.userInfo.id,
    //             "deptName": that.data.userInfo.nickName,
    //             "deptType": that.data.userInfo.userType,
    //             "operatorId": that.data.userInfo.id,
    //             "operatorName": that.data.userInfo.nickName
    //         };
    //     // /fms/accountsRechargeOrder/helpRecharge
    //     app.basePost(app.U({
    //         m: 'fms',
    //         c: 'accountsRechargeOrder',
    //         a: 'helpRecharge'
    //     }), data, res => {
    //         that.setData({
    //             orderNo: res.data.orderNo
    //         })
    //         console.log(that.data.orderNo)
    //     }, (res) => {})
    // },

    //授权
    mandate() {
        var that = this;
        wx.getUserProfile({
            desc: '获取微信头像和昵称',
            success: (res) => {
                this.setData({
                    chatName: res.userInfo.nickName,
                    chatHead: res.userInfo.avatarUrl
                });
                wx.login({
                    success(res) {
                        that.setData({
                            jsCode: res.code
                        })
                        console.log('代充值')
                        that.ensureRecharge()
                    }
                })
            },
            fail: res => {
                wx.showToast({
                    icon: 'none',
                    title: '获取用户信息失败'
                })
                console.log("获取用户信息失败", res)
            }
        })
    },

    // 充值
    ensureRecharge(e) {
        console.log('orderno' + this.data.orderNo)
        var that = this,
            data = {
                "jsCode": that.data.jsCode,
                "nickName": that.data.userInfo.nickName,
                "orderNo": that.data.orderNo,
                "wxLiteSettingsId": 4,
            };
        // /fms/accountsRechargeOrder/helpRecharge
        app.baseForm(app.U({
            m: 'fms',
            c: 'accountsRechargeOrder',
            a: 'helpRechargeToPay'
        }), data, res => {
            console.log(123)
            console.log(res)
            var payInfo = JSON.parse(res.data.payInfo),
                id = res.data.order_no;
            wx.requestPayment({
                timeStamp: payInfo.timeStamp,
                nonceStr: payInfo.nonceStr,
                package: payInfo.package,
                signType: payInfo.signType,
                paySign: payInfo.paySign,
                success(res) {
                    console.log(res)
                    var type = 1;
                    that.queryRecharge(id,type)
                },
                fail(res) {
                    console.log(res)
                    var type = 2;
                    that.queryRecharge(id,type)
                }
            })
        }, (res) => {})
    },

    // 查询充值订单详情
    queryRecharge(id,type) {
        var that = this;
        // /fms/accountsRechargeOrder/queryRechargeOrderInfo
        app.baseGet(app.U({
            m: 'fms',
            c: 'accountsRechargeOrder',
            a: 'queryRechargeOrderInfo',
            q: {
                orderno: id
            }
        }), res => {
            console.log(res)
            that.setData({
                orderInfo: res.data,
                dialogTwo: type == 1 ? false : true,
                dialogOne: type == 1 ? true : false
            })
        }, (res) => {})
    },

    // 取消保证金充值
    cancelecharge(id) {
        console.log(id)
        var that = this;
        // /fms/accountsRechargeOrder/cancelechargeOrder
        app.baseGet(app.U({
            m: 'fms',
            c: 'accountsRechargeOrder',
            a: 'cancelechargeOrder',
            q: {
                orderno: id
            }
        }), res => {
            console.log(res)
            that.setData({
                orderInfo: res.data,
                dialogThree: true
            })
        }, (res) => {})
    },

    exit() {
        wx.exitMiniProgram({
            success: function (res) {
                console.log(res)
            }
        })
    },

    return () {
        wx.switchTab({
            url: '/pages/home/index'
        })
    },

    //关闭充值详情
    dialogOne() {
        this.setData({
            dialogOne: false,
            dialogTwo: false,
            dialogThree: false
        })
    },

    //取消订单
    cancelOrder(e) {
        this.cancelecharge(e.currentTarget.dataset.id)
    },

    onShareAppMessage: function () {
        console.log(this.data.orderNo)
        return {
            title: this.data.title,
            path: '/pages/subrogation/gfPayment/index?type=2&title=' + JSON.stringify(this.data.money) + '&orderNo=' + this.data.orderNo
        }
    }
})